package com.zzb.framework.starter.xxljob.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.google.common.collect.Lists;
import com.xxl.job.admin.dao.XxlJobLogDao;
import com.zzb.framework.common.BasePage;
import com.zzb.framework.common.PageSearchForm;
import com.zzb.framework.query.GenericQueryDAO;
import com.zzb.framework.starter.xxljob.dao.XxlJobLogMapper;
import com.zzb.framework.starter.xxljob.form.XxlJobLogSearchForm;
import com.zzb.framework.starter.xxljob.model.XxlJobInfo;
import com.zzb.framework.starter.xxljob.model.XxlJobLog;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzb.framework.starter.xxljob.vo.XxlJobLogVO;
import com.zzb.framework.utils.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * .___  ___.  _______  __  ____    ____  __         __   __       ___
 * |   \/   | |   ____||  | \   \  /   / |  |       |  | |  |     /   \
 * |  \  /  | |  |__   |  |  \   \/   /  |  |       |  | |  |    /  ^  \
 * |  |\/|  | |   __|  |  |   \_    _/   |  | .--.  |  | |  |   /  /_\  \
 * |  |  |  | |  |____ |  |     |  |     |  | |  `--'  | |  |  /  _____  \
 * |__|  |__| |_______||__|     |__|     |__|  \______/  |__| /__/     \__\
 * <p>
 * XxlJobLog Service实现
 *
 * @author : ZZB
 * @since : 2019-09-10 11:24:16
 */
@Service
@Slf4j
@DS("xxljob")
public class XxlJobLogService extends ServiceImpl<XxlJobLogMapper, XxlJobLog> {
    @Autowired
    private GenericQueryDAO genericQueryDAO;

    public <S extends PageSearchForm> BasePage<XxlJobLog> queryPage(S searchForm) {
        return genericQueryDAO.queryPage(searchForm, XxlJobLog.class);
    }

    @Autowired
    private XxlJobInfoService xxlJobInfoService;
    public BasePage<XxlJobLogVO> page(XxlJobLogSearchForm searchForm) {
        if (searchForm.getLogStatus() != null) {

            if (searchForm.getLogStatus() == 1) {
                searchForm.setEq_handleCode(200);
            } else if (searchForm.getLogStatus() == 2) {
                searchForm.setAdditionalCondition(" AND ( t.trigger_code NOT IN (0, 200) OR t.handle_code NOT IN (0, 200))");
            } else if (searchForm.getLogStatus() == 3) {
                searchForm.setEq_triggerCode(200);
                searchForm.setEq_handleCode(0);
            }
        }

        if (StringUtils.isBlank(searchForm.getOrderBy())) {
            searchForm.setOrderBy("t.trigger_time DESC");
        }
        BasePage<XxlJobLog> xxlJobLogBasePage = queryPage(searchForm);
        List<XxlJobLog> xxlJobLogs = xxlJobLogBasePage.getContent();
        Map<Integer , XxlJobInfo> map = new HashMap<>();
        if(CollectionUtils.isNotEmpty(xxlJobLogs)){
            List<Integer> jobIds = xxlJobLogs.stream().map(XxlJobLog::getJobId).collect(Collectors.toList());
            List<XxlJobInfo> xxlJobInfos = Lists.newArrayList(xxlJobInfoService.listByIds(jobIds));
            map.putAll( xxlJobInfos.stream().collect(Collectors.toMap(XxlJobInfo::getId, o -> o )));
        }

        List<XxlJobLogVO> xxlJobLogVOs =  xxlJobLogs.stream().map( e ->{
            XxlJobLogVO vo = new XxlJobLogVO();
            BeanUtils.copyProperties(e,vo);
            XxlJobInfo xxlJobInfo = map.get(vo.getJobId());
            if(xxlJobInfo != null) {
                vo.setJobCron(xxlJobInfo.getJobCron());
                vo.setJobDesc( xxlJobInfo.getJobDesc());
            }
            return vo;
        }).collect(Collectors.toList());
        BasePage<XxlJobLogVO> xxlJobLogVOBasePage = new BasePage<>(xxlJobLogVOs,searchForm.getPage(),searchForm.getPageSize(),xxlJobLogBasePage.getTotalElements());
        return xxlJobLogVOBasePage;
    }
}
